package com.l.web.service.system.config;

import com.l.web.SystemConfigure;
import org.l.qiao.Cluster;
import org.l.qiao.ClusterUtil;
import org.l.qiao.Validate;
import org.l.qiao.el.ELConvert;
import com.l.web.service.ServiceSupport;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ConfigDetailService extends ServiceSupport {

    private static final Logger LOGGER = Logger.getLogger(ConfigDetailService.class);
    private ConfigDAO configDAO;

    public void setConfigDAO(ConfigDAO configDAO) {
        this.configDAO = configDAO;
    }

    public String execute() {
        String id = form.getString("id");
        LOGGER.debug("查看配置信息：id=" + id);

        if (Validate.check(Validate.OBJECTID, id)) {
            Cluster config = configDAO.findById(id);

            if (config.isEmpty()) {
                LOGGER.error("所查看的配置不存在");
                error(404);
                return NONE;
            }
            List options = new ArrayList();
            ELConvert elConvert = new ELConvert();
            elConvert.toList(config.getMap(Config.OPTION, new HashMap()), "", options);
            context.put("config", config);
            context.put("check", ClusterUtil.getCheck(Config.class));
            context.put("options", options);
            context.put("baseConfig", SystemConfigure.configure);
            return "detail";
        } else {
            error(404);
            LOGGER.error("非法查看配置信息：id=" + id);
            return NONE;
        }

    }

}
